package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1358 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numberOfSubstrings(String s) {
            if (s.length() < 3) return 0;

            int[] posArray = new int[3];
            int left = 0, right = 0, count = 0;
            while (right < s.length()) {
                char cur = s.charAt(right);
                int pos = cur - 'a';
                posArray[pos]++;

                while (isMatch(posArray)) {
                    count += s.length() - right;
                    //收缩窗口左边界
                    posArray[s.charAt(left++) - 'a']--;
                }

                ++right;
            }

            return count;
        }

        public boolean isMatch(int[] posArray) {
            return posArray[0] > 0 && posArray[1] > 0 && posArray[2] > 0;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.numberOfSubstrings("abc"));
    }

    public static void main(String[] args) throws Exception {
        LC1358 an = new LC1358();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
